package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcPlanMapper;
import com.csun.cmny.provider.dto.PlanQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcPlan;
import com.csun.cmny.provider.model.domain.OmcShifts;
import com.csun.cmny.provider.service.OmcPlanService;
import com.csun.cmny.provider.service.OmcShiftsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class OmcPlanServiceImpl extends BaseService<OmcPlan> implements OmcPlanService {

    @Resource
    private OmcPlanMapper omcPlanMapper;

    @Resource
    private OmcShiftsService omcShiftsService;


    @Override
    public Integer savePlan(OmcPlan omcPlan, LoginAuthDto loginAuthDto) {

        //放入操作者信息
        omcPlan.setUpdateInfo(loginAuthDto);
        omcPlan.setGroupId(loginAuthDto.getGroupId());

        //校验方案名称
        checkName(omcPlan);

        if (omcPlan.isNew()) {
            //插入排班方案
            int insertPlanResult = omcPlanMapper.insertSelective(omcPlan);
            if(omcPlan.getShiftsList() != null && omcPlan.getShiftsList().size()>0){
                List<OmcShifts> shiftsList = omcPlan.getShiftsList();
                //将操作者信息放入排班项中
                for(int i = 0,j=shiftsList.size();i<=j-1;i++){
                    shiftsList.get(i).setPlanid(omcPlan.getId());
                    shiftsList.get(i).setUpdateInfo(loginAuthDto);
                    shiftsList.get(i).setGroupId(loginAuthDto.getGroupId());
                }
                //批量插入排班项
                int insertShiftsResult = omcShiftsService.batchSave(shiftsList);
                if(insertShiftsResult < 0){
                    throw new OmcBizException(ErrorCodeEnum.OMC10035001);
                }
                if(insertPlanResult <= 0){
                    throw new OmcBizException(ErrorCodeEnum.OMC10035002);
                }
            }
            return insertPlanResult;
        } else {
            int updatePlanRet = omcPlanMapper.updateByPrimaryKeySelective(omcPlan);
            //方案被采用——只能更新
            if(omcPlan.getIsUsed() >= 1){
                if(omcPlan.getShiftsList() == null || omcPlan.getShiftsList().size() == 0){
                    return updatePlanRet;
                }
                omcShiftsService.batchUpdate(omcPlan.getShiftsList());
            //方案没有被采用——可以进行添加班次、删除班次
            }else{
                //先删除原有的数据
                omcShiftsService.deleteByPlanId(omcPlan.getId());
                if(omcPlan.getShiftsList() == null || omcPlan.getShiftsList().size() == 0){
                    return updatePlanRet;
                }
                //再插入
                List<OmcShifts> shiftsList = omcPlan.getShiftsList();
                for(int i=0;i<=shiftsList.size()-1;i++){
                    shiftsList.get(i).setUpdateInfo(loginAuthDto);
                    shiftsList.get(i).setGroupId(loginAuthDto.getGroupId());
                    shiftsList.get(i).setPlanid(omcPlan.getId());
                }
                int insertShiftsRet = omcShiftsService.batchSave(shiftsList);
                if(updatePlanRet > 0 && insertShiftsRet >= 0){
                    return 1;
                }else{
                    return 0;
                }
            }
            return updatePlanRet;
        }
    }

    @Override
    public Integer deleteById(Long planId) {

        //先校验该方案是否被使用，若使用过无法删除
        OmcPlan omcPlan = omcPlanMapper.selectByPrimaryKey(planId);
        if(omcPlan != null && omcPlan.getIsUsed() > 0){
            throw new OmcBizException(ErrorCodeEnum.OMC10035004);
        }
        //先删除排班方案
        int deletePlanRet = omcPlanMapper.deleteByPrimaryKey(planId);
        //删除排班方案下的排班项
        int deleteShiftsRet = omcShiftsService.deleteByPlanId(planId);
        if(deletePlanRet >0 && deleteShiftsRet >= 0){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public List<OmcPlan> queryListAll(PlanQueryDto PlanQueryDto) {

        //查询排班方案
        List<OmcPlan> omcPlanList = omcPlanMapper.queryListAll(PlanQueryDto);
        if(omcPlanList.size()>0){
            //放入排班项
            omcPlanList = omcShiftsService.setShifts(omcPlanList);
        }
        return omcPlanList;
    }

    public void checkName (OmcPlan omcPlan){
        Example example = new Example(OmcPlan.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name",omcPlan.getName());
        criteria.andEqualTo("groupId",omcPlan.getGroupId());
        if(omcPlan.getId() != null){
            criteria.andNotEqualTo("id",omcPlan.getId());
        }
        int count = selectCountByExample(example);
        if (count > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10035008);
        }
    }
}
